<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\Admin\ActionButtonTrait;

class GoodsType extends Model
{
    use Filterable, ActionButtonTrait;

    protected $append = ['actionButton'];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $actions = ['edit','delete'];
    protected $actionUri = 'goodstype';


    //
    protected $fillable = [
        'merchant_id', 'goods_name', 'pid','state'
    ];

    /**
     * @return array
     *  获取分类列表
     */

    static public function getcates(){

        $user = auth('admin')->user();
        $userid = $user ['id'];
        if($user['type'] == 4){
            $userid = $user->parent_id;
        }
        $cateArr = self::get();
        $cateArr = self::query()->where('merchant_id',$userid)->get();
        return $cateArr;
    }
    /**
     * 组织分类数据
     * 递归实现无限极分类
     */
    static function makecates($data,$pid=0,$level=0){
        $arr = [];
        foreach ($data as $item){
            if($item->pid==$pid){
                $item->level = $level;
                $arr[]=$item;
                $arr_tmp = self::makecates($data,$item->id,$level+1);
                $arr = array_merge($arr,$arr_tmp);//数组合并
            }
        }
        return $arr;
    }
}

